package com.aijk.ehm.service;

import com.aijk.base.entity.PagingRequest;
import com.aijk.commons.utils.Utils;

import com.aijk.ehm.dao.mybatis.SemRecordMapper;
import com.aijk.ehm.model.SemRecord;
import com.aijk.ehm.model.SemRecordExample;
import com.aijk.ehm.model.qo.SemRecordQo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 测量记录
 */
@Service
public class SemRecordService {
    private static Logger logger = LoggerFactory.getLogger(SemRecordService.class);

    @Autowired
    private SemRecordMapper mapper;

    public SemRecordExample getExample(SemRecordQo qo) {
        SemRecordExample example = new SemRecordExample();
        SemRecordExample.Criteria criteria = example.createCriteria();
        if (qo != null) {
            if (Utils.isNotEmpty(qo.getSortway())) example.setOrderByClause(qo.getSortway());
            if (qo.getStartdate()!= null) criteria.andMeterdateGreaterThanOrEqualTo(qo.getStartdate());
            if (qo.getEnddate()!= null) criteria.andMeterdateLessThanOrEqualTo(qo.getEnddate());
            if (qo.getResidentId()!= null) criteria.andResidentIdEqualTo(qo.getResidentId());
            if (qo.getUid()!= null) criteria.andUidEqualTo(qo.getUid());
            if (qo.getIdcard()!= null) criteria.andIdcardEqualTo(qo.getIdcard());
            if (qo.getDvtype()!= null) criteria.andDvtypeEqualTo(qo.getDvtype());
            if (qo.getResidentIds()!= null) criteria.andResidentIdIn(qo.getResidentIds());
            if (qo.getCompanyId()!= null) criteria.andCompanyIdEqualTo(qo.getCompanyId());
            if (qo.getDataId()!= null) criteria.andDataIdEqualTo(qo.getDataId());
        }
        return example;
    }

    public int count(SemRecordQo qo) {
        return mapper.countByExample(getExample(qo));
    }

    public int save(SemRecord bean) {
        return mapper.insertSelective(bean);
    }

    public int delete(Long id) {
        return mapper.deleteByPrimaryKey(id);
    }

    public int delete(SemRecordQo qo) {
        return mapper.deleteByExample(getExample(qo));
    }

    public int update(SemRecord bean) {
        return mapper.updateByPrimaryKeySelective(bean);
    }

    public SemRecord findOne(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    public List<SemRecord> find(SemRecordQo qo) {
        return mapper.selectByExample(getExample(qo));
    }

    public PageInfo<SemRecord> page(PagingRequest pagingRequest, SemRecordQo qo) {
        PageHelper.startPage(pagingRequest.getPageNum() , pagingRequest.getPageSize());
        return new PageInfo<SemRecord>(mapper.selectByExample(getExample(qo)));
    }
}